package com.Web.servlet;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Properties;

public class DataSourceUtils {
    //数据源
    private  static DataSource dataSource;
    static {
        //读取配置文件的对象
        Properties properties = new Properties();
        try {
            //读取配置文件
            properties.load(DataSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //初始化数据源
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public  static Connection getConnection(){
        if(dataSource !=null){
            try{
                return  dataSource.getConnection();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return  null;
    }
}
